﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace SqlHelper
{
    public class SqlHelper
    {
        
        //<summary>
        //执行SQL语句，返回影响的记录数
        //</summary>
        public int ExecuteSql(string strSql, string conString)
        {
            using (SqlConnection connection = new SqlConnection(conString))
            {
                using (SqlCommand cmd = new SqlCommand(strSql, connection))
                {
                    connection.Open();
                    int rows = cmd.ExecuteNonQuery();
                    return rows;
                }
            }

        }
        /// <summary>
        /// GetDataSet
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="conString"></param>
        /// <returns>dataSet</returns>
        public static DataSet GetDataSet(string strSql, string conString)
        {
            using (SqlConnection connection = new SqlConnection(conString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                connection.Open();
                SqlCommand command = new SqlCommand(strSql, connection);
                command.CommandType = CommandType.Text;
                adapter.SelectCommand = command;

                DataSet dataSet = new DataSet();
                adapter.Fill(dataSet);
                return dataSet;
            }

        }

        /// <summary>
        /// InsertTableData
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="conString"></param>
        public static void InsertTableData(string strSql, string conString)
        {

            using (SqlConnection connection = new SqlConnection(conString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(strSql, connection);
                command.CommandType = CommandType.Text;

                command.ExecuteNonQuery();
            }
        
        }
        /// <summary>
        /// delete data from tables
        /// </summary>
        /// <param name="strSql"></param>
        /// <param name="conString"></param>
        public static void DeleteTableData(string strSql, string conString)
        {

            using (SqlConnection connection = new SqlConnection(conString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(strSql, connection);
                command.CommandType = CommandType.Text;

                command.ExecuteNonQuery();
            }

        }
    }
}
